开发者

Simple Javascript question about "if" condition

开发者 https://www.devze.com 2023-01-19 23:47 出处:网络
if (selectedCountry == \"USA\") { $(\"#state\").show(); } How can I change name for my div #state with the same condition?
if (selectedCountry == "USA") {
  $("#state").show();
}

How can I change name for my div #state with the same condition? (to add name="NewName" to my div id="state")

Smth like:

i开发者_如何学编程f (selectedCountry == "USA") {
  $("#state").show();
  $("#state").name("NewName");      <-- ?
}


Use .attr() to set attributes on an element, for this:

if (selectedCountry == "USA") {
  $("#state").show().attr("name", "NewName");
}

If setting many at once, you can also pass an object, for example:

if (selectedCountry == "USA") {
  $("#state").show().attr({ name: "NewName" });
}

I'm assuming you meant a <select> here with a dropdown, since a <div> won't be serialized into a form a name attribute isn't useful or valid, if this isn't the select, just find the form element and perform the same .attr() call.


if (selectedCountry == "USA") {
  $("#state").show();
  $("#state").attr("name","NewName");
}
0

精彩评论

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

关注公众号