简体   繁体   English

将HTML值分配给JavaScript变量

[英]assigning an HTML value to a JavaScript variable

I have the following Javascript function that allows me to add a new row to my table: 我有以下Javascript函数,允许我向我的表添加一个新行:

$(function() {

var $table = $('table.pv-data'),
    counter = 1;

$('a.add-author').click(function(event){
    event.preventDefault();
    counter++;            
var newRow = 
        '<tr>' + 
            '<td><input type="text" name="id' + counter + '"/></td>' +
            '<td><select name="state' + counter + '"/><OPTION value= "persil">Persil</OPTION><OPTION value= "other">Other</OPTION></select></td> ' +
            '<td><input type="text" name="longitude' + counter + '"/></td>' +
            '<td><input type="text" name="latitude' + counter + '"/></td>' +
            '<td><input type="text" name="altitude' + counter + '"/></td>' +
            '<td><input type="text" name="module_tilt' + counter + '"/></td>' +
            '<td><a href="#" class="remove">remove</a></td>'
        '</tr>';
    $table.append(newRow);
    });
});

Everything is working fine, just the part where I have to select between the options:"Persil" or "Other" is not working. 一切都很好,只是我必须在选项之间选择的部分:“Persil”或“Other”不起作用。 it's not showing the different options. 它没有显示不同的选项。

The following lines need to be replaced 需要更换以下行

  1. '<td><select name="state' + counter + '"/>...' . '<td><select name="state' + counter + '"/>...' Is not correct as you are closing the select tag too early. 因为您过早关闭选择标记,所以不正确。
  2. '<td><a href="#" class="remove">remove</a></td>' . '<td><a href="#" class="remove">remove</a></td>' You are missing + at the end. 你最后错过了+

by these replacements: 通过这些替换:

  1. '<td><select name="state' + counter + '">...' . '<td><select name="state' + counter + '">...'
  2. '<td><a href="#" class="remove">remove</a></td>' +

Note: Replacement#2 is pointed out in the comments. 注意:评论中指出了替换#2。

Take away the self-closing tag from the end of the opening select tag and remove the space between the value= "persil" in both of the option elements. 从开始select标记的末尾删除自闭标记,并删除两个option元素中value= "persil"之间的空格。 Also add a + to concatenate the final </tr> . 还要添加一个+来连接最终的</tr>

var newRow = 
    '<tr>' + 
        '<td><input type="text" name="id' + counter + '"/></td>' +
        '<td><select name="state' + counter + '"><OPTION value="persil">Persil</OPTION><OPTION value="other">Other</OPTION></select></td> ' +
        '<td><input type="text" name="longitude' + counter + '"/></td>' +
        '<td><input type="text" name="latitude' + counter + '"/></td>' +
        '<td><input type="text" name="altitude' + counter + '"/></td>' +
        '<td><input type="text" name="module_tilt' + counter + '"/></td>' +
        '<td><a href="#" class="remove">remove</a></td>' +
    '</tr>';

Having the self closing tag at the end of an element (the slash at the end, as in <img src="..." /> ) is only for elements without an independent closing tag, so for something like <select> you do not need it as this function is fulfilled by the </select> . 在元素末尾使用自闭关标记(结尾处的斜杠,如<img src="..." /> )仅适用于没有独立结束标记的元素,因此对于<select>东西不需要它,因为</select>实现了这个功能。

You have a problem in this line: 你在这一行有问题:

'<td><select name="state' + counter + '"/><OPTION value= "persil">P

The select have it own closing tag it must be like this select有自己的结束标记,必须是这样的

'<td><select name="state' + counter + '"><OPTION value= "persil">P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM