How to set input box value in a div
I've a function that takes a div id and se开发者_StackOverflow社区ts the values of few input controls inside it and finally show the div. my function looks like this:
function updateDiv(div_id)
{
$("#"+div_id+" :input[ID^='FIRST']").val('First Name');
$("#"+div_id+" :input[ID^='LAST']").val('Last Name');
$("#"+div_id).show();
}
but it does not seem to be working. Am I missing something?
It's normally a bad idea to pass ids around as strings. At the very least, you should not select the element three times!
Also, attributes (such as ID) should not be uppercase.
function updateDiv(div_id) {
var div = $('#'+div_id);
div.find(':input[id^="FIRST"]').val('First Name');
div.find(':input[id^="LAST"]').val('Last Name');
div.show();
}
Here I have a working solution:
The attribute value is case-sensitive, so for example, if id="firstname", the selector would not match.
<div id="mydiv">
<input type="text" id="FIRSTNAME"/>
<input type="text" id="LASTNAME"/>
</div>
$(document).ready(function(){
var div_id = "mydiv";
$("#"+div_id+" :input[ID^='FIRST']").val('First Name');
$("#"+div_id+" :input[ID^='LAST']").val('Last Name');
});
jsFiddle
加载中,请稍侯......
精彩评论