如何更新模板单选按钮

时间:2015-07-10 02:57:53

标签: javascript jquery

是否有一种优雅的方式来更新'描述'使用JQuery?

<div>
    <div class="radio">
        <label>
            <input name="somename" type="radio">description
        </label>
    </div>
    <div class="well">
        <div class="line">Something</div>
        <div class="line">Something else</div>
    </div>
</div>

我在标签上尝试了文字(&#39;新描述&#39;),但之后也删除了输入。有没有在我的javascript中没有序列化html的解决方案?

jsfiddle

我想要实现的是在源html中有一个模板单选按钮,我可以动态克隆,更新和追加。我不确定是否有更好的模式可以实现动态的选择列表。

3 个答案:

答案 0 :(得分:1)

用跨度围绕它,然后更新跨度中的文本。像这样

<div>
    <div class="radio">
        <label>
            <input name="somename" type="radio"><span id="needsUpdate">description</span>
        </label>
    </div>
    <div class="well">
        <div class="line">Something</div>
        <div class="line">Something else</div>
    </div>
</div>

然后javascript

$('#needsUpdate').text('new description');

答案 1 :(得分:1)

您在这里:只需使用class ArrayDemo { public static void main(String[] args) { int[] anArray; int ageCount = 0; int age; String filename = "ageData.dat"; anArray = new int[50]; /* The file has 50 numbers which represent an employee's age */ try { Scanner infile = new Scanner(new FileInputStream(filename)); while (infile.hasNext()) { age = infile.nextInt(); ageCount += 1; System.out.println(ageCount + ". " + age + "\n"); /* * When i run just this, the output is correct... * * But i don't want the output here, i just want to gather * the information from the file and place it at the bottom inside * the method displayAges().*/ } infile.close(); } catch (IOException ex) { ageCount = -1; ex.printStackTrace(); } public void listOfAges() { System.out.print(" I want to display the data gathered from ageData.dat in this method "); System.out.print(" Also, i receive this error message when i try: 'Illegal modifier for parameter listOfAges; only final is permitted' ") } } }

&#13;
&#13;
label for
&#13;
$('#somelabel').text('new description');
&#13;
&#13;
&#13;

希望这有帮助。

答案 2 :(得分:0)

您可以使用this.nodeType === 3,但除非您真的无法更改HTML以将文本换行,否则我不会推荐它。

$('label')
  .contents()
  .filter(function() {
    return this.nodeType === 3 && $(this).index() != 0;
  }).replaceWith('New text');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <div class="radio">
        <label>
            <input name="somename" type="radio">description
        </label>
    </div>
</div>