亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

HTML/JS:在 JavaScript 中更改選擇選項值

HTML/JS:在 JavaScript 中更改選擇選項值

HUH函數 2022-12-22 14:46:57
我有一個帶有兩個選項的下拉選擇菜單?;旧衔沂褂?JavaScript 獲取所選下拉選項的值并粘貼到文本區域。這是我的代碼:$(document).ready(function () {  $('#pdSev').change(function () {    $('#textarea').html($("#pdSev option:selected").text());  })});<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script><select id="pdSev" name="pdSev">  <option selected disabled class="test">Incident Severity</option>  <option value="test">Test</option>  <option value="test2">Test2</option></select><textarea id="textarea" class="textarea is-rounded has-fixed-size" placeholder="Example"></textarea>到目前為止,當我從選擇菜單中選擇一個項目時,這目前是有效的,選項值被復制到紋理字段中。但是,我希望將選項值替換為段落而不是只包含一個詞,例如:"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."在 JS 中做這樣的事情最好的方法是什么?
查看完整描述

2 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

在此示例中,我已將 的設置value為該test option段落。


現在textarea包含值而不是文本。


$(document).ready(function () {

    var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

    $('option[value="test"]').val(text);


    $('#pdSev').change(function () {

        $('#textarea').html($("#pdSev option:selected").val());

    })

});

<body>

  <select id="pdSev" name="pdSev">

    <option selected disabled class="test">Incident Severity</option>

    <option value="test">Test</option>

    <option value="test2">Test2</option>

  </select>

  <textarea id="textarea" class="textarea is-rounded has-fixed-size" placeholder="Example"></textarea>

</body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>



查看完整回答
反對 回復 2022-12-22
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

根據您的問題和另一個答案的評論,這是您實現目標的方法;


$(document).ready(function () {

    $('#pdSev').change(function () {

        if ($('#pdSev').val() == 'test') {

            var text = 'additional text';

            $('#textarea').val(text);

        } else if ($('#pdSev').val() == 'test2') {

            var text = 'text for this statement';

            $('#textarea').val(text);

        }

    })

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<select id="pdSev" name="pdSev">

  <option selected disabled class="test">Incident Severity</option>

  <option value="test">Test</option>

  <option value="test2">Test2</option>

</select>

<textarea id="textarea" class="textarea is-rounded has-fixed-size" placeholder="Example"></textarea>


從這里您可以繼續構建您的else if聲明并滿足您的需求。例如,如果您添加了一個,test3您將更新if statement為看起來像;


if ($('#pdSev').val() == 'test') {

    var text = 'additional text';

    $('#textarea').val(text);

} else if ($('#pdSev').val() == 'test2') {

    var text = 'text for this statement';

    $('#textarea').val(text);

} else if ($('#pdSev').val() == 'test3') {

    var text = 'here is a new statement';

    $('#textarea').val(text);

}

.val()請注意和之間的區別.text()。


.val()將在 VALUE 內部查找,其中.text()將在選項標簽之間查找 TEXT;如下;


<option value="VALUE">TEXT</option>

因此,在if statement我提供的內容中,它將.text()值放在文本框中——但是如果你想讓它把.val()值放在文本框中,只需更新我必須.text()去的地方.val()——如果這有意義的話?


查看完整回答
反對 回復 2022-12-22
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號