Thymeleaf onclick将td值发送到javascript函数

时间:2015-10-07 14:19:26

标签: javascript java jquery html thymeleaf

我正在使用“Thymeleaf”,我想将值发送到javascript,我是新手,我正在尝试下面的代码:

onclick="getPropId('${properties.id}')"

和功能

getPropId(inputID){alert(inputId);}

但我没有获得实际价值。

4 个答案:

答案 0 :(得分:7)

以上问题现已解决,我们需要使用Thymeleaf特定的语法。

th:onclick="'getPropId(\'' + ${properties.id} + '\');'"

现在如果它在javascript函数中显示正确的properties.id。

function getPropId(inputID){
    alert(inputID);
}                           

答案 1 :(得分:3)

要传递多个参数,您可以使用以下内容:

import java.util.*;
import java.io.*;

import org.json.JSONException;
import org.json.JSONObject;

public class Project {
    private static Scanner input;
    static JSONObject obj = new JSONObject();
    static String choice = "";
    static int i = 0;
    final static String file_path = "d:\\1\\MiniDB.txt";

    public static void menu() throws IOException, JSONException {
        System.out.println("Please choose one of the following commands:");
        System.out.println("L (Listing)");
        System.out.println("I (Insert)");
        System.out.println("D (delete)");
        System.out.println("S (search)");
        System.out.println("M (modify)");
        System.out.println("W (write)");
        System.out.println("Q (quit with saving)");
        choice = input.nextLine();
        if (choice.equalsIgnoreCase("I")) {
            Insert();
        }
        if (choice.equalsIgnoreCase("L")) {
            Listing();
        }
        if (choice.equalsIgnoreCase("W")) {
            write(obj);
        }
        if (choice.equalsIgnoreCase("Q")) {
            i = 1;
            System.out.println("End");
        }
    }

    public static void main(String[] args) {
        try {
            input = new Scanner(System.in);
            while (i == 0) {
                menu();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void Listing() // List records from MiniDB.txt file
    {
        try {
            File file = new File(file_path);
            input = new Scanner(file);
        } catch (IOException ioException) {
            System.err.println("Cannot open file.");
            System.exit(1);
        }
        if (input.hasNext()) { // If there are records in the file print them,
                                // else print none found
            while (input.hasNext()) // while there is more to read
            {
                String firstName = input.next();
                String lastName = input.next();
                String phoneNumber = input.next();

                System.out.printf("%-13s%-13s%s%n", firstName + ",", lastName,
                        phoneNumber);
            }
        } else {
            System.out.printf("No records found");
        }
    } // end method Listing

    public static void Insert() // insert a record
    {
        try {
            System.out.println("Last Name: ");
            obj.put("Last Name", input.nextLine());
            System.out.println("First Name: ");
            obj.put("First Name", input.nextLine());
            System.out.println("Telephone Number: ");
            obj.put("Telephone Number", input.nextLine());
        } catch (Exception e) {
            System.exit(1);
        }
    }

    public static void write(JSONObject obj2) throws IOException, JSONException {
        File file = new File(file_path);
        PrintWriter outputStream = new PrintWriter(new FileWriter(file, true));
        outputStream.printf("%n%s %s %s", obj.get("Last Name"),
                obj.get("First Name"), obj.get("Telephone Number"));
        System.out.println("changes Done.");
        outputStream.close();
    }

    public static void Search() {
    }

    {
    }
} // end class Project 

答案 2 :(得分:1)

带文字替换的清洁器||:

th:onclick="|getPropId('${properties.id}');|"

多种情况:

th:onclick="|getPropId('${var1}','${var2}');|"

答案 3 :(得分:0)

我通过声明th:attribute

解决了同样的问题
<div class="row" th:each="data,i : ${obj}">
    <a href="javascript:void(0);" th:attr="onclick='loadDetails(\'' + ${data.objId}+'\')'">
</a>
</div>