前端小技巧

8 篇文章 0 订阅

select2下拉框的使用

<tr>
    <td>
        <select id="simHatyubusyo" th:field="*{simHatyubusyo}" class="select2" disabled>
            <option th:each="c:*{hachusakiTantobushoList}" 
                    th:value="${c.id}" th:text="${c.text}"></option>
        </select>
    </td>
</tr>
<tr>
    <td>
        <select id="simHatyubusyo2" th:field="*{simHatyubusyo2}" class="select2" disabled>
            <option th:each="c:*{hachusakiTantobushoList}" 
                    th:value="${c.id}" th:text="${c.text}"></option>
        </select>
    </td>
</tr>
<tr>
    <td>
        <select id="simHatyubusyo3" th:field="*{simHatyubusyo3}" class="select2" disabled>
            <option th:each="c:*{hachusakiTantobushoList}" 
                    th:value="${c.id}" th:text="${c.text}"></option>
        </select>
    </td>
</tr>
<tr>
    <td>
        <select id="simHatyubusyo4" th:field="*{simHatyubusyo4}" class="select2" disabled>
            <option th:each="c:*{hachusakiTantobushoList}" 
                    th:value="${c.id}" th:text="${c.text}"></option>
        </select>
    </td>
</tr>
$("#simHatyubusyo,#simHatyubusyo2,#simHatyubusyo3,#simHatyubusyo4").each(function(item) {
	// 要想select2中放入的数据
	var sdataList = [
	    {id: 1, text:'OPS-COFFEE-1'},
	    {id: 2, text:'OPS-COFFEE-2'},
	    {id: 3, text:'OPS-COFFEE-3'}
	]
	
	// 向select2下拉列表中放入数据之前,先清空掉原有的数据
	// ⭕⭕⭕
	// 只能通过这个select2自带的API来清空下拉列表
	$(this).empty();
	
    // 向当前的select2中放入数据
    $(this).select2({
        // data: singleData.hachusakiTantobushoList,
        data: sdataList 
    });
	
    // 移除当前下拉框的不可用属性
    $(this).removeAttr("disabled");
	
	// 获取下拉框选中的文字
    let word = $(this).select2('data')[0].text;
	
	// 设置select2被默认选中的值
	$(`select2对象`).val(需要被选中的id).select2();
});

// 隐藏select2的搜索框,让下拉列表没有检索功能,变为普通的下拉列表
$('select2对象').select2({
    minimumResultsForSearch: -1
});

for (var i = 0; i < jtv010NyusatsukaishaFormCount; i++) {
    // 枝番(1から連番する)
    sinEdano = singleData.jtv010NyusatsukaishaFormList[i].sinEdano - 1;
    // 入札会社コード
    sinNyusyacd = singleData.jtv010NyusatsukaishaFormList[i].sinNyusyacd.toString();
    
    // 向select2下拉框中放入值
    $(`#sinNyusyacd${sinEdano}`).select2({
        // eval()函数的妙用
        data: eval(`singleData.nyusatsuKaishaList${sinEdano + 1}`),
    });
    
    // 指定select2下拉框要选中的值
    $(`#sinNyusyacd${sinEdano}`).val(sinNyusyacd).select2();
}

eval函数的使用

for (var i = 0; i < gijyutsuTableCount; i++) {
    eval(`submitData.jtaTanSyu${i + 1} = 
		$('#jtaTanSyu${i + 1}').prop('checked') ? 1 : 0`);
    
    if ($(`#jtaTanSyu${i + 1}`).prop('checked')) {
        jtaTanSyacd = $(`#jtaTanSyacd${i + 1}`).val();
    }
}

checkbox设置单选

<table id="gijyutsuTable" class="border_table">
    <tr>
        <td style="width: 50px;">
            <input id="jtaTanSyu1" th:checked="*{jtaTanSyu1} eq '1'" type='checkbox' />
        </td>
    </tr>
    <tr>
        <td style="width: 50px;">
            <input id="jtaTanSyu2" th:checked="*{jtaTanSyu2} eq '1'" type='checkbox' /> 
        </td>
    </tr>
    <tr>
        <td style="width: 50px;">
            <input id="jtaTanSyu3" th:checked="*{jtaTanSyu3} eq '1'" type='checkbox' />
        </td>
    </tr>
</table>
$("#gijyutsuTable input:checkbox").on("click", function(obj) {
    // 当点击的时候给所有的checkbox取消选中
    $("#gijyutsuTable input:checkbox").each(function(index, chekboxItem) {
        $(chekboxItem).prop('checked', false);
    });
    
    // 只给当前checkbox设置为选中
    $(obj.target).prop('checked', true);
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值