如何在javascript中获取标签值

时间:2016-09-01 10:15:25

标签: javascript jquery

我正在使用下面的代码基于json数据创建动态tr / td。

for (i = 0; i < dataPoolJSON.length; i++) {

  html += "<tr id ='row" + i + "' value ='" + dataPoolJSON[i].msisdn + "'><td><div class='group'><label id='label" + i + "' for='numView" + dataPoolJSON[i].msisdn + "' class='ch-control'>";
  html += "<input id='numView" + dataPoolJSON[i].msisdn + "' type='radio' class='iradio_minimal' value='" + i + "' name='msisdnSelected'/>" + dataPoolJSON[i].msisdn;
  html += "</label></div></td><td class='hidden-xs'><a id='viewUsage" + i + "' class='viewUsage' onclick='viewDataPoolUsage(this," + dataPoolJSON[i].msisdn + ");return false;' href=''>View Usage</a>";
  html += "<div class='pool-usage hidden'></div></td>";
  html += "<td><span id='dataAllowed" + i + "'><select>";

  if (dataPoolJSON[i].userSetting != null) {
    html += "<option selected='selected' value='" + dataPoolJSON[i].userSetting.alertPercentageData1 + "'>" + dataPoolJSON[i].userSetting.alertPercentageData1 + "</option>";
  }
  html += "</select></span></td></tr>";
}
$('#data-pool tbody').html(html);

现在单击单选按钮我需要获取msisdn值和当前选项选择的值,但它没有给我所需的答案。我得到当前选项的选择值,但不是msisdn。

function submitNewDataUsagealerts() {

    var jsonSubmitData = [];
    var selectedData = document.getElementsByName("msisdnSelected");
    var i = selectedData.length-1;
    for ( j=0; j <= i; j++ ) {

        if(selectedData[j].checked) {

            var valueCurrent = $('#dataAllowed'+selectedData[j].value).find('option:selected').val();
            var row = $('#label'+selectedData[j].value).val();
            item = {}
            item [0] = valueCurrent;
            item [1] = selectedData[j].value;
        }
    }
 }

3 个答案:

答案 0 :(得分:0)

var msg = document.getElementById('lbltxt').innerHTML;
alert(msg);
<label id="lbltxt" style="display:none">Test</label>

答案 1 :(得分:0)

HTML代码

 <label id="mylabel" />

JS代码

var lavelValue = $("#mylabel").text();

答案 2 :(得分:0)

获得&#34;值&#34;从标签中使用.text()而不是.val()

var row = $('#label'+selectedData[j].value).text();

由于您的标签包含输入和所需文本,您可以将文本放在容器中,以便在不包含输入的情况下引用它:

标签代码目前(已删除无关代码):

html += "<label id='label" + i + "' for='numView" + dataPoolJSON[i].msisdn + "' class='ch-control'>";
html += "<input id='numView" + dataPoolJSON[i].msisdn + "' type='radio' class='iradio_minimal' value='" + i + "' name='msisdnSelected'/>";
html += dataPoolJSON[i].msisdn;
html += "</label>";

将其更改为:

html += "<label id='label" + i + "' for='numView" + dataPoolJSON[i].msisdn + "' class='ch-control'>";
html += "<input id='numView" + dataPoolJSON[i].msisdn + "' type='radio' class='iradio_minimal' value='" + i + "' name='msisdnSelected'/>" ;
html += "<span>" + dataPoolJSON[i].msisdn + "</span>";
html += "</label>";

然后您的选择器可以是:

var row = $('#label'+selectedData[j].value + ">span").text();

或者,当您已使用label for=时,将输入移出标签:

html += "<input id='numView" + dataPoolJSON[i].msisdn + "' type='radio' class='iradio_minimal' value='" + i + "' name='msisdnSelected'/>" ;
html += "<label id='label" + i + "' for='numView" + dataPoolJSON[i].msisdn + "' class='ch-control'>";
html += dataPoolJSON[i].msisdn
html += "</label>";

然后您的选择器可以是:

var row = $('#label'+selectedData[j].value).text();