다음과 같은 내용이있는 표 머리글 셀이 있습니다.
<th rowspan="1" colspan="1">
<select>
<option value="date" selected="selected">Date</option>
<option value="number">Number</option>
</select>
</th>
이렇게 선택한 값을 얻으려고합니다.
var $selectors = $("#myTable thead tr th").each(function(index)
{
var cell = $(this).html();
console.log ("Cell:" + cell.toSource());
var value = cell.options[cell.selectedIndex].value;
});
하지만 선택한 값을 읽으려고하면 오류가 발생합니다.
TypeError: cell.selectedIndex is undefined
위와 같이 cell.toSource ()를 사용하여 셀을 콘솔에 덤프하면 다음과 같습니다.
Cell:(new String("<select><option value=\"\"></option><option value=\"date\" selected=\"selected\">Date</option><option value=\"number\">Number</option></select>"))
나는 그 'new String'이 어디에서 왔는지, 그것이 내 문제의 원인인지 또는 그 안에 들어가는 방법을 모릅니다. 선택한 값을 어떻게 얻을 수 있습니까?
당신은 당신이 사용 selected value
하는 .val
대로 직접 사용할 수 있습니다jquery
$(function () {
$('#myTable thead tr th').each(function () {
alert($(this).find("select").val());
});
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다