2015-12-02 69 views
0
<div class="form-group"> 
    <label>State/County</label> 
    <input class="form-control" name="state_county" type="text" maxlength="50" /> 
</div> 

<div class="form-group"> 
    <label>Country <strong class="reqstar">*</strong></label> 
    <select class="form-control" name="country" onchange="getTelephone(this.selectedIndex)" style="width:257px"> 
     <option value="UK">UK</option> 
     <option value="USA">USA</option> 
    </select> 
</div> 

当我把我的country选择USA,我想修改我的state_county的标签的HTML。添加额外的HTML的标签上选择改变

所需的HTML:<label>State/County <strong class="reqstar">*</strong></label>

这是代码我使用的线:,

$("input[name=state_county]").closest("form-group").find("label").html("State/County <strong class=\"reqstar\">*</strong>"); 

我使用closest()去最近的容器元素,然后使用find()得到回落到标签,然后使用html()修改它的内容,但它不起作用?

+0

是否有可能修改HTML呢?如果是这样的话,最简单的解决方案是将带有'id'的' *'元素添加到'label'中并隐藏它。然后在Country选择的变化上切换它的可见性。 –

回答

1
$("input[name=state_county]").closest(".form-group").find("label").html("State/County <strong class=\"reqstar\">*</strong>"); 

当你正在寻找的形式,组,你需要的.,表示你正在寻找一个类名。

2

更简单的方法做同样的将是: -

$("input[name=state_county]").siblings("label").html("State/County <strong class=\"reqstar\">*</strong>");