简体   繁体   English

Java:如何将值从 javascript 设置为检票口组件文本字段

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

I am using javascript to get a value from client-side using a button that is not binded to a wicket component (ie, it doesn't have a wicket-id).我正在使用 javascript 从客户端使用未绑定到 wicket 组件的按钮获取值(即,它没有 wicket-id)。 Now, I need to set that value to a textfield (textarea) - that is a wicket-component, but unable to do that.现在,我需要将该值设置为文本字段 (textarea) - 这是一个检票口组件,但无法做到这一点。 If I set value in a simple textarea (not a wicket component) then it works fine.如果我在一个简单的 textarea(不是 wicket 组件)中设置值,那么它工作正常。 But I need that value in the wicket component so that I can get its value when the form submits.但是我需要在 wicket 组件中使用该值,以便在提交表单时可以获得它的值。

Javascript isn't working on the wicket component. Javascript 不适用于 wicket 组件。

Javascript method: Javascript方法:

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

html: 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>

Java:爪哇:

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);
 }

Here, I can set value in the textarea id=e2 but not on id=e1.在这里,我可以在 textarea id=e2 中设置值,但不能在 id=e1 上设置值。

The problem come from this line:问题来自这一行:

 textArea.setEnabled(true).setOutputMarkupId(true)

This will generate a custom id for the TEXTAREA (for Wicket Ajax).这将为 TEXTAREA(用于 Wicket Ajax)生成一个自定义 id。 So, the id is changed and your javascript won't work anymore, because that uses the 'e1' id that is replaced.因此, id已更改,您的 javascript 将不再工作,因为它使用了已替换的 'e1' id。

You can add a custom data-attribute and get the component by that attribute.您可以添加自定义数据属性并通过该属性获取组件。

For example:例如:

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

And then (for example, using jquery, something like this)然后(例如,使用 jquery,类似这样的)

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

Or, if you don't need ajax, skip calling the setOutputMarkupId或者,如果您不需要 ajax,请跳过调用setOutputMarkupId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM