簡體   English   中英

Java:如何將值從 javascript 設置為檢票口組件文本字段

[英]Java: How to set a value from javascript to a wicket component textfield

我正在使用 javascript 從客戶端使用未綁定到 wicket 組件的按鈕獲取值(即,它沒有 wicket-id)。 現在,我需要將該值設置為文本字段 (textarea) - 這是一個檢票口組件,但無法做到這一點。 如果我在一個簡單的 textarea(不是 wicket 組件)中設置值,那么它工作正常。 但是我需要在 wicket 組件中使用該值,以便在提交表單時可以獲得它的值。

Javascript 不適用於 wicket 組件。

Javascript方法:

var text2 = document.getElementById("e2");
var text1 = document.getElementById("e1");
text1.value = "hello";
text2.value = "hello";

html:

<wicket:panel>
<form wicket:id="form" id="form" onsubmit="submit(event,this);">
<div wicket:id="myPanel">

   <input type="button" value="Verify" name="B3" onclick=GetTemplate() />

   <p><textarea name="S2" id="e2"></textarea></p>

   <p><textarea wicket:id="e1" name="S1" id="e1" ></textarea></p>

   <input type="submit" class="submitForm" wicket:id="submit" wicket:message="value:Submit" name="submit" />

</div>
</form>
</wicket:panel>

爪哇:

public class MyPanel  extends Panel {
 public MyPanel(...){
   ...
   Form<?> form = new Form("form", new Model());
   container = new WebMarkupContainer("myPanel");

   textArea = new TextArea<String>("e1", new Model());
   textArea.setEnabled(true).setOutputMarkupId(true).
   textArea.setOutputMarkupPlaceholderTag(true);
   textArea.setEscapeModelStrings(false);
   textArea.add(new ErrorIndicator());

   submitButton = (Button) new Button("submit") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                message = textArea.getModelObject();
            }
        }.setEnabled(true);
   container.add(textArea);
   container.add(submitButton);
   form.add(container);
   add(form);
 }

在這里,我可以在 textarea id=e2 中設置值,但不能在 id=e1 上設置值。

問題來自這一行:

 textArea.setEnabled(true).setOutputMarkupId(true)

這將為 TEXTAREA(用於 Wicket Ajax)生成一個自定義 id。 因此, id已更改,您的 javascript 將不再工作,因為它使用了已替換的 'e1' id。

您可以添加自定義數據屬性並通過該屬性獲取組件。

例如:

 <p><textarea wicket:id="e1" name="S1" data-id="e1" ></textarea></p>

然后(例如,使用 jquery,類似這樣的)

  $("TEXTAREA[data-id='e1']").val("hello");

或者,如果您不需要 ajax,請跳過調用setOutputMarkupId

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM