Browser Independence issue Code Working for IE but

2019-02-20 23:27发布

问题:

if i run the same code for IE every thing is fine i am getting the value of selectedId where as for firefox and chrome it is giving values Undefine.

----------------- code -------------------------------

<html>
<head>
<script type="text/javascript">

function createSelected()
{
var value;
var theForm = document.hell;

for (var i = 0; i < theForm.length; i++)
{
var e = theForm.elements[i];
if ((e.type == "hidden") && (e.value == "false"))
{
       console.log("the value of selected IDS="+e.selectedId);
    if (e.selectedId!= undefined )
    {   
        Value = ", "+e.selectedId;

    }
}
}
console.log( Value);
}


</script>
</head>
<body>
<form name="hell">

<h1>This working only with IE not with FireFox and Crome  </h1>
<br/>
<br/>
<input type="hidden" selectedId="heyya1"  name="item1" value="false">h1</input>
<input type="hidden" selectedId="heyya2"  name="item2" value="false">h2</input>
<input type="hidden" selectedId="heyya3"  name="item3" value="false">h3</input>
<input type="hidden" selectedId="heyya4"  name="item4" value="false">h4</input>
<input type="hidden" selectedId="heyya5"  name="item5" value="false">h5</input>
<input type="hidden" selectedId="heyya6"  name="item6" value="false">h6</input>
<input type="button" onclick=createSelected() value="find the values"></input>
</form>


</body>

---------------------------code end-----------------------------------------

please help in in this why cant we use other parameter( like Selectedid inside HTML tag) in FireFox like we can do it in IE.

thanks in advance..

回答1:

In Chrome (sorry, I'm using a computer that doesn't have FF, and I'm not going to install it just for this question) it works if you use .getAttribute(). So:

// replace
e.selectedId;
// with
e.getAttribute("selectedId");

Or, in your function:

var id = e.getAttribute("selectedId");
console.log("the value of selected IDS="+id );
if (id != null)
{   
    value += ", "+id;
}

Also, note that JavaScript is case sensitive, so your variable value is not the same as Value, and you were saying Value = where you probably meant value +=. And you may want to use quotes around your element attributes: onclick="createSelected()".



回答2:

Try reading the selectedId as below. Use getAttribute method

alert("the value of selected IDS="+e.getAttribute('selectedId'));