Javascript getElementBy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16526970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:01:14  来源:igfitidea点击:

Javascript getElementBy

javascripthtml

提问by Satia Anbalagan

I have a problem to detect the option selection where instead of using getElementById i want to use getElementByValue.Does it exist?? this is my code

我有一个问题来检测选项选择,而不是使用 getElementById 我想使用 getElementByValue。它存在吗?这是我的代码

     <script type="text/javascript">
     var total;


function oneway(){

document.getElementById("return_date").style.visibility = "hidden";

document.getElementById("return_time").style.visibility = "hidden";

}

function return_way(){

document.getElementById("return_date").style.visibility = "visible";

document.getElementById("return_time").style.visibility = "visible";

}

function state_price(){
var way=document.getElementById("direction").value;

var placefrom = document.getElementById("state_from").value;
var placeto   = document.getElementById("state_to").value;

if (direction == "oneway"){
    if ((placefrom=="Batu Pahat" && placeto=="Alor Setar") ||(placefrom =="Alor Setar" && placeto=="Batu Pahat")){

        total=55.00;
        document.booking.total_price.value = total;


    }else if ((placefrom=="Batu Pahat" && placeto=="Bachok")||(placefrom=="Bachok" && placeto=="Batu Pahat"))
    {

        total=60.00;
        document.booking.total_price.value = total; 


    }else if ((placefrom=="Batu Pahat" && placeto=="A Famosa") || (placefrom=="A Famosa" && placeto=="Batu Pahat"))
    {

        total=65.50;
        document.booking.total_price.value = total;

    }else if ((placefrom=="Batu Pahat" && placeto=="Bahau")||(placefrom=="Bahau" && placeto=="Batu Pahat"))
    {
        total=70.00;
        document.booking.total_price.value = total;

    }else if ((placefrom=="Batu Pahat" && placeto=="Bentong") ||(placefrom=="Bentong" && placeto=="Batu Pahat"))
    {
        total=75.50;
        document.booking.total_price.value = total;
    }else if ((placefrom=="Batu Pahat" && placeto=="Kubang Kerian") ||(placefrom=="Kubang Kerian" && placeto=="Batu Pahat"))
    {
        total=80.00;
        document.booking.total_price.value = total;

    }

    else
    {

        document.booking.txttotal.value = "Fail";
        window.alert("Please enter a different destination");

    }
}
else
{



///something else

}
}
</script>

and this is the part of my html coding

这是我的 html 编码的一部分

 <tr > 
 <td>Travel Direction:</td>
 <td >

 <input type="radio" name ="direction" value = "oneway"  onclick = "oneway()"/>One Way
 <input type="radio" name ="direction" value = "return"  onclick = "return_way()"/>Return

 </td>
 </tr>

i don't want to use the name as i'm using it in my php coding.so i don;t want to use it.Is there any way where i can do my javascript as i mentioned above (getElementByValue)?Thank You in advance

我不想使用这个名字,因为我在我的 php 编码中使用它。所以我不想使用它。有什么办法可以像我上面提到的那样做我的 javascript (getElementByValue)?谢谢提前

采纳答案by Hoffmann

Well if you can't set the id attribute you can apply a CSS class and use:

好吧,如果您无法设置 id 属性,则可以应用 CSS 类并使用:

document.getElementsByClassName

document.getElementsByClassName(needs IE 9.0 or higher)

document.getElementsByClassName(需要 IE 9.0 或更高版本)

Or you can just traverse the whole DOM checking for the value attribute. But this would be slow as hell and very bug-prone without using jQuery or similar.

或者,您可以遍历整个 DOM 检查 value 属性。但是如果不使用 jQuery 或类似的东西,这会很慢,而且很容易出错。

Edit: Quentin answer is better, document.querySelector works on IE8 or higher and you don't need to create CSS classes.

编辑:Quentin 的回答更好,document.querySelector 适用于 IE8 或更高版本,您不需要创建 CSS 类。

回答by Quentin

i want to use getElementByValue. Does it exist??

我想使用 getElementByValue。存在吗??

No. You could write your own (looping over all the elements in the document and testing them) or, if you care about initial value, rather than current value, you could use querySelector/ querySelectorAllwith an attribute selector.

不。您可以编写自己的(循环遍历文档中的所有元素并测试它们),或者,如果您关心初始值而不是当前值,则可以将querySelector/querySelectorAll属性选择器一起使用

Since you are using radio buttons, which don't change values (only checked status) under normal circumstances, that should work fine.

由于您使用的单选按钮在正常情况下不会更改值(仅检查状态),因此应该可以正常工作。

document.querySelector('input[value=oneway]');

回答by epascarello

One problem here is you are using getElementById and you are using the name, that is not going to work.

这里的一个问题是您正在使用 getElementById 并且您正在使用名称,这是行不通的。

Personally I would check to see if the first radio button [one way] is checked.

我个人会检查是否选中了第一个单选按钮 [one way]。

var isOneWay = document.forms[0].direction[0].checked;

Than in the if statement, you can use the Boolean value to determine the logic.

比在 if 语句中,您可以使用布尔值来确定逻辑。

回答by AlexandruSerban

Use jQuery it has a value selector, ex: $('input["value=your-value"]')

使用 jQuery 它有一个值选择器,例如: $('input["value=your-value"]')