开发者

jQuery - change value of attribute

开发者 https://www.devze.com 2023-03-15 18:57 出处:网络
I don\'t know why this code not working good. I want change value of attribute \"value\" from \"---\" to \"Day\"

I don't know why this code not working good. I want change value of attribute "value" from "---" to "Day"

<select name="0-birthdate_day" id="id_0-birthdate_day">
<option value="0">---</option>
</select>

$("#id_0-b开发者_如何学Pythonirthdate_day").children().eq(1).attr("value").replace('0', 'Day')

Thanks


$("#id_0-birthdate_day").children().eq(1).attr("value",function(i,val){
    return val.replace('0', 'Day');
});

You can give a function to .attr(). The return value will be the updated value.


Try this:

$("#id_0-birthdate_day option[value='0']").text('Day');


According to your question you want to change the text from --- to Day.

This should do the trick:

jQuery("#id_0-birthdate_day option[value='0']").text('Day');

Basically it will find the option with value 0 and replace its text label with Day.


$("#id_0-birthdate_day").find("option[value=0]").html('Day')


To change the "value" from "---" to "Day" you would use the .html function, not the attr() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消